package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.Comment;
import com.example.mapper.CommentMapper;
import com.example.service.CommentService;
import org.springframework.stereotype.Service;


import java.util.Date;
import java.util.List;

@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Override
    public List<Comment> getCommentsByArticleId(Integer articleId) {
        return baseMapper.selectList(new QueryWrapper<Comment>().eq("article_id", articleId));
    }

    @Override
    public Comment addComment(Integer articleId, Comment comment) {
        // 设置文章ID
        comment.setArticleId(articleId);
        // 设置创建时间
        comment.setCreatedAt(new Date());
        // 设置点赞数默认为0
        comment.setLikeCount(0);
        // 保存评论
        save(comment);
        return comment;
    }
}

